package com.xys.auth.service.impl;

import com.github.pagehelper.PageHelper;
import com.xys.auth.bean.Member;
import com.xys.auth.bean.Orders;
import com.xys.auth.bean.Traveller;
import com.xys.auth.mapper.OrdersMapper;
import com.xys.auth.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class OrdersServiceImpl implements IOrderService {

    @Autowired
    OrdersMapper ordersMapper;

    @Override
    public List<Orders> findAll(Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        return ordersMapper.findAll();
    }

    @Override
    public Orders findById(String id) {
        return ordersMapper.findById(id);
    }

    @Override
    public int save(Orders orders) {
        orders.setId(UUID.randomUUID().toString().replaceAll("-",""));
        orders.setMemberId(UUID.randomUUID().toString().replaceAll("-",""));
        orders.setProductId(UUID.randomUUID().toString().replaceAll("-",""));
        orders.setOrderStatus(0);
        return ordersMapper.insert(orders);

    }

    @Override
    public void addProductToOrders(String id, String[] ids) {

        for(int i=0;i<ids.length;i++){

            ordersMapper.updateProduct(id,ids[i]);
            System.out.println("订单编号是"+id);

        }





    }

    @Override
    public int saveTraveller(Traveller traveller, String id) {

        traveller.setId(UUID.randomUUID().toString().replaceAll("-",""));
        traveller.setTravellerType(1);


        String travellerId=traveller.getId();



        ordersMapper.insertTravellerAndOrders(id,travellerId);

        return  ordersMapper.insertTraveller(traveller);


    }

    @Override
    public int saveMember(Member member, Orders orders) {

        member.setId(UUID.randomUUID().toString().replaceAll("-",""));

        String memberId=member.getId();
        String id=orders.getId();

        ordersMapper.updateMember(id,memberId);

        return  ordersMapper.insertMember(member);

    }

    @Override
    public void update(String id) {

        ordersMapper.update(id);

    }

    @Override
    @Transactional
    public void delete(String id) {
        ordersMapper.delete(id);
        ordersMapper.deleteOrderTravellerById(id);
    }
}

